<?php
class Zendvn_Controller_Action extends Zend_Controller_Action{
	
	public  function init(){
		/*echo '<pre>';
		print_r($this->_request->getBaseUrl());
		echo '</pre>';*/
		//$module = $this->_request->getModuleName();
		//$pathTemplate = APPLICATION_PATH . '/templates/' . $module . '/default';
		
		$pathTemplate = APPLICATION_PATH . '/public';
		
		$fileConfig = 'template.ini';
		$sectionConfig = 'template';
		
		$category = new Category_Model_Index();
		
		$this->view->list_category = $category->listItem(); 
		
		$chanel = new Chanel_Model_Index();		
		
		$page = 1;
		
		$pagesize = 5;
		
		$arr = array(				
			"page"=> $page,
			"pagesize"=> $pagesize,																
		);
		
		$this->view->list_chanel = $chanel->listItemAll($arr);	

		//Read Cookie Account
		
		$registration = new Registration_Model_Auth();
		
		$registration->checkLoginCookie();
		
		$this->createTemplate($pathTemplate, $fileConfig, $sectionConfig);
		
	}
	
	public function createTemplate($pathTemplate = null, $fileConfig = 'template.ini', $sectionConfig = 'template' ,$type = 'new' ){
		
		
		if($type == 'new'){
			$headLink 			= new Zend_View_Helper_HeadLink();
			$containerHeadLink 	= $headLink->getContainer();
			$containerHeadLink->exchangeArray(array());
			
			$headMeta 			= new Zend_View_Helper_HeadMeta();
			$containerHeadMeta 	= $headMeta->getContainer();
			$containerHeadMeta->exchangeArray(array());
			
			$headScript			= new Zend_View_Helper_HeadScript();
			$containerHeadScript= $headScript->getContainer();
			$containerHeadScript->exchangeArray(array());
		}
		/*echo '<pre>';
		print_r($containerHeadMeta);
		echo '</pre>';*/
		
		
	/*	$view = new Zend_View();
		$view->empty();*/
		//$dirTemplate = APPLICATION_PATH . '/templates/front/default';
		$dirTemplate = $pathTemplate;
		
    	//$fileName = $dirTemplate . '/template.ini';
    	$fileName = $dirTemplate . '/' . $fileConfig;
    	
    	//$config = new Zend_Config_Ini($fileName, 'template');
    	$config = new Zend_Config_Ini($fileName, $sectionConfig);
    	
    	$config = $config->toArray();
    	/*echo '<pre>';
    	print_r($config);
    	echo '</pre>';*/
    	
		if(isset($config['title'])){
			$this->view->headTitle($config['title']);
		}
		
		if(isset($config['metaHttp'])){
			foreach ($config['metaHttp'] as $key => $val){				
				$tmp = explode('|',$val);				
				$this->view->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
			}
		}
		
		if(isset($config['metaName'])){
			foreach ($config['metaName'] as $key => $val){				
				$tmp = explode('|',$val);				
				$this->view->headMeta()->appendName($tmp[0],$tmp[1]);
			}
		}
		
		$baseUrl =  $this->_request->getBaseUrl();
		$dirTemplateUrl = $baseUrl .$config['url']; 
		
		
		
		$dirCss = $dirTemplateUrl . $config['dirCss'];
		if(isset($config['fileCss'])){
			foreach ($config['fileCss'] as $key => $val){	
							
				//$this->view->headLink()->appendStylesheet($dirCss . $val, 'screen');
				$this->view->headLink()->appendStylesheet($dirCss . $val, 'screen');				
			}
		}
		
		$dirJs = $dirTemplateUrl . $config['dirJs'];
		if(isset($config['fileJs'])){
			foreach ($config['fileJs'] as $key => $val){
				$this->view->headScript()->appendFile($dirJs . $val,'text/javascript');
			}
		}
		
		$dirImg = $dirTemplateUrl . '/images';
		$this->view->dirImg = $dirImg;
			
		$option = array('layout'=>$config['layout'],'layoutPath'=>$dirTemplate);
		Zend_Layout::startMvc($option);
		
	}
}